function e(e, a) {
    e.setData({
        totalHeight: 0
    }), t(e, wx.createCanvasContext("tempCanvas"), 0, a);
}

function t(e, a, i, o) {
    i < e.data.longImageSrcs.length ? wx.getImageInfo({
        src: e.data.longImageSrcs[i],
        success: function(c) {
            var n = c.width / (750 * e.deviceRatio), s = c.width / n, h = c.height / n, r = e.data.totalHeight, l = e.data.totalHeight + h;
            e.setData({
                totalHeight: l
            }), a.drawImage(e.data.longImageSrcs[i], 0, r, s, h), a.draw(!0), t(e, a, i + 1, o);
        }
    }) : setTimeout(o, 500);
}

function a(e, t) {
    var a = e.data, o = a.minScale, c = a.maxScale, n = i(t.touches, 2), s = n[0], h = n[1], r = void 0, l = void 0, g = void 0, d = void 0, m = void 0;
    if (1 === t.touches.length && (r = s.clientX - e.touchX, l = s.clientY - e.touchY, 
    e.imgLeft = e.startX + r, e.imgTop = e.startY + l, e.setData({
        photoTop: e.imgTop,
        photoLeft: e.imgLeft
    })), t.touches.length >= 2) {
        var f = e.frameHeight ? e.frameHeight : 1e3 * e.deviceRatio;
        d = h.clientX - s.clientX, m = h.clientY - s.clientY, g = Math.sqrt(d * d + m * m), 
        e.newScale = e.oldScale + .005 * (g - e.oldDistance), e.newScale <= o && (e.newScale = o), 
        e.newScale >= c && (e.newScale = c), e.scaleWidth = e.newScale * e.initScaleWidth, 
        e.scaleHeight = e.newScale * e.initScaleHeight, e.imgLeft = 750 * e.deviceRatio / 2 - e.newScale * e.initLeft, 
        e.imgTop = f / 2 - e.newScale * e.initTop, e.setData({
            photoTop: e.imgTop,
            photoLeft: e.imgLeft,
            photoWidth: e.scaleWidth,
            photoHeight: e.scaleHeight
        });
    }
}

var i = function() {
    function e(e, t) {
        var a = [], i = !0, o = !1, c = void 0;
        try {
            for (var n, s = e[Symbol.iterator](); !(i = (n = s.next()).done) && (a.push(n.value), 
            !t || a.length !== t); i = !0) ;
        } catch (e) {
            o = !0, c = e;
        } finally {
            try {
                !i && s.return && s.return();
            } finally {
                if (o) throw c;
            }
        }
        return a;
    }
    return function(t, a) {
        if (Array.isArray(t)) return t;
        if (Symbol.iterator in Object(t)) return e(t, a);
        throw new TypeError("Invalid attempt to destructure non-iterable instance");
    };
}(), o = getApp();

Page({
    data: {
        page: "",
        imageNotChoosed: !1,
        imgViewHeight: 0,
        longImageSrcs: [],
        totalHeight: 0,
        whichDeleteShow: 99999,
        frameSrcs: [ {
            src: "frame1.png",
            title: "文艺小清新"
        }, {
            src: "frame2.png",
            title: "Happy Birthday"
        }, {
            src: "frame3.png",
            title: "素描花环"
        }, {
            src: "frame4.png",
            title: "文艺小清新"
        }, {
            src: "frame5.png",
            title: "卡通小屋"
        }, {
            src: "frame6.png",
            title: "爱心相框"
        }, {
            src: "frame7.png",
            title: "心形云朵"
        }, {
            src: "frame8.png",
            title: "爱心花环"
        }, {
            src: "frame9.png",
            title: "拍立得相框"
        }, {
            src: "frame10.png",
            title: "文艺小清新"
        }, {
            src: "frame11.png",
            title: "贴纸"
        } ],
        frameSrc: "",
        isFrameChoose: !1,
        photoSrc: "",
        frameHeight: 0,
        minScale: .5,
        maxScale: 2,
        photoWidth: 0,
        photoHeight: 0,
        photoLeft: 0,
        photoTop: 0,
        readuSave: !1,
        ad:[]
    },
    onShow(){
        this.getRandomAd()
        getApp().chaping()
    },
    onLoad: function(e) {
        getApp().getRandomAd(true,this)
        this.getRandomAd(true)
        this.device = o.globalData.myDevice, this.deviceRatio = this.device.windowWidth / 750, 
        this.setData({
            page: e.choosed,
            longImageSrcs: [],
            imgViewHeight: this.device.windowHeight - 160 * this.deviceRatio
        }), "longImages" === e.choosed && this.addImages();
    },
    onShareAppMessage: function() {
        return {
            title: "快速拼图",
            path: "pages/spliceMenu/spliceMenu"
        };
    },
    async addImages () {
        var e = this;
        wx.chooseImage({
            sourceType: [ "album", "camera" ],
            success: async function(t) {
                var a = e.data.longImageSrcs;
                let imgData = await getApp().checkImg(a[0])
                console.log(imgData)
                if(!imgData||imgData.result.errCode!==0){
                    wx.showModal({
                    confirmText: '确定',
                    content: '图片内容不合法，请重新选择',
                    showCancel: false
                    })
                    return
                }
                a = a.concat(t.tempFilePaths), e.setData({
                    imageNotChoosed: !1,
                    longImageSrcs: a,
                    readuSave: !0
                });
            },
            fail: function(t) {
                e.setData({
                    imageNotChoosed: !0
                });
            }
        });
    },
    gotoDelete: function(e) {
        this.longTap = !0, this.deleteId = e.target.dataset.idx, this.setData({
            whichDeleteShow: this.deleteId
        });
    },
    deleteImg: function() {
        var e = this.data.longImageSrcs;
        e.splice(this.deleteId, 1), this.setData({
            longImageSrcs: e,
            whichDeleteShow: 99999
        });
    },
    quitDelete: function() {
        this.getRandomAd()
        this.longTap ? this.longTap = !1 : this.setData({
            whichDeleteShow: 99999
        });
    },
    addFrame: function() {
        this.getRandomAd()
        this.setData({
            isFrameChoose: !0
        });
    },
    chooseFrame: function(e) {
        var t = this;
        this.getRandomAd()
        wx.getImageInfo({
            src: "../../image/frame/" + e.currentTarget.dataset.src,
            success: function(a) {
                var i = a.width / (750 * t.deviceRatio);
                t.frameWidth = a.width / i, t.frameHeight = a.height / i, t.setData({
                    frameHeight: t.frameHeight,
                    isFrameChoose: !1,
                    frameSrc: "../../image/frame/" + e.currentTarget.dataset.src,
                    readuSave: !0
                });
            }
        });
    },
    closeFrameChoose: function() {
        this.getRandomAd()
        this.setData({
            isFrameChoose: !1
        });
    },
    addPhoto: function() {
        var e = this;
        wx.chooseImage({
            count: 1,
            sourceType: [ "album", "camera" ],
            success: async function(t) {
                let imgData = await getApp().checkImg(t.tempFilePaths[0])
                console.log(imgData)
                if(!imgData||imgData.result.errCode!==0){
                    wx.showModal({
                    confirmText: '确定',
                    content: '图片内容不合法，请重新选择',
                    showCancel: false
                    })
                    return
                }
                e.setData({
                    photoSrc: t.tempFilePaths[0]
                }), wx.getImageInfo({
                    src: t.tempFilePaths[0],
                    success: function(t) {
                        e.frameHeight = e.frameHeight ? e.frameHeight : 1e3 * e.deviceRatio, e.initRatio = t.height / e.frameHeight, 
                        e.initRatio < t.width / (750 * e.deviceRatio) && (e.initRatio = t.width / (750 * e.deviceRatio)), 
                        e.scaleWidth = t.width / e.initRatio, e.scaleHeight = t.height / e.initRatio, e.startX = 750 * e.deviceRatio / 2 - e.scaleWidth / 2, 
                        e.startY = e.frameHeight / 2 - e.scaleHeight / 2, e.oldScale = 1, e.initScaleWidth = e.scaleWidth, 
                        e.initScaleHeight = e.scaleHeight, e.setData({
                            photoWidth: e.scaleWidth,
                            photoHeight: e.scaleHeight,
                            photoTop: e.startY,
                            photoLeft: e.startX,
                            readuSave: !0,
                            frameHeight: e.frameHeight
                        });
                    }
                });
            },
            fail: function(t) {
                e.setData({
                    imageNotChoosed: !0
                });
            }
        });
    },
    uploadScaleStart: function(e) {
        var t = this, a = void 0, o = void 0, c = i(e.touches, 2), n = c[0], s = c[1];
        if (t.touchX = n.clientX, t.touchY = n.clientY, t.imgLeft = t.startX, t.imgTop = t.startY, 
        e.touches.length >= 2) {
            var h = t.frameHeight ? t.frameHeight : 1e3 * t.deviceRatio;
            t.initLeft = (750 * t.deviceRatio / 2 - t.imgLeft) / t.oldScale, t.initTop = (h / 2 - t.imgTop) / t.oldScale, 
            a = s.clientX - n.clientX, o = s.clientY - n.clientY, t.oldDistance = Math.sqrt(a * a + o * o);
        }
    },
    uploadScaleMove: function(e) {
        a(this, e);
    },
    uploadScaleEnd: function(e) {
        var t = this;
        t.oldScale = t.newScale || t.oldScale, t.startX = t.imgLeft || t.startX, t.startY = t.imgTop || t.startY;
    },
    saveImgToPhone: function() {
        var t = this;
        if ((t.data.longImageSrcs || t.data.frameSrc) && wx.showLoading({
            title: "保存中",
            mask: !0
        }), "longImages" === t.data.page) new Promise(function(a, i) {
            e(t, a);
        }).then(function(e) {
            wx.canvasToTempFilePath({
                canvasId: "tempCanvas",
                success: function(e) {
                    console.log(e.tempFilePath), wx.navigateTo({
                        url: "/pages/preview/preview?imageurl=" + e.tempFilePath
                    }), wx.hideLoading();
                }
            });
        }); else if ("photoFrame" === t.data.page) {
            var a = t.frameHeight ? t.frameHeight : 1e3 * t.deviceRatio;
            t.setData({
                totalHeight: a
            });
            var i = wx.createCanvasContext("tempCanvas"), o = Math.max(-t.data.photoLeft * t.initRatio / t.oldScale, 0), c = Math.max(-t.data.photoTop * t.initRatio / t.oldScale, 0), n = t.device.windowWidth * t.initRatio / t.oldScale, s = a * t.initRatio / t.oldScale, h = t.device.windowWidth, r = a, l = Math.max(t.data.photoLeft, 0), g = Math.max(t.data.photoTop, 0);
            i.drawImage(t.data.photoSrc, o, c, n, s, l, g, h, r), i.drawImage(t.data.frameSrc, 0, 0, h, r), 
            i.draw(), setTimeout(function() {
                wx.canvasToTempFilePath({
                    canvasId: "tempCanvas",
                    success: function(e) {
                        console.log(e.tempFilePath), wx.navigateTo({
                            url: "/pages/preview/preview?imageurl=" + e.tempFilePath
                        }), wx.hideLoading();
                    }
                });
            }, 100);
        }
    }
});